package com.meet.controller;

import com.meet.bizservice.SettingsService;
import com.meet.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import java.util.Arrays;

@RestController
@RequestMapping("/members")
@CrossOrigin
@Validated
public class MeetMemberController {

    @Autowired
    private SettingsService settingsService;

    @GetMapping("/level-info")
    public Result<?> getMemberLevelInfo() {

        return settingsService.getMemberLevelInfo();
    }

    @PostMapping("/upgrade")
    public Result<?> upgradeMemberLevel(@RequestBody @Validated MemberUpgradeRequest request) {
        // 验证会员等级参数
        if (!Arrays.asList("premium", "gold").contains(request.getLevel())) {
            return Result.error(400, "无效的会员等级参数", "INVALID_MEMBER_LEVEL");
        }
        return settingsService.upgradeMemberLevel(request.getLevel());
    }

    // Request DTO
    public static class MemberUpgradeRequest {

        @NotBlank(message = "会员等级不能为空")
        private String level;

        // Getters and Setters
        public String getLevel() {

            return level;
        }

        public void setLevel(String level) {

            this.level = level;
        }

    }

}
